package com.xydzxt.www.controller.student;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import java.io.IOException;
import java.util.List;
import com.xydzxt.www.model.Regist;
import com.xydzxt.www.model.User;
import com.xydzxt.www.service.RegistService;
import com.xydzxt.www.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@Controller
public class LoginController {
    @Autowired
    RegistService registService;
    @Autowired
    UserService userService;

    @PostMapping("/login")
    public void login(HttpServletRequest request, HttpServletResponse resp) throws IOException {

        int isUser =Integer.valueOf(request.getParameter("isUser"));
        String sfzh=request.getParameter("sfzh");
        String password=request.getParameter("password");
        boolean isLogin=false;

        if (isUser==1){
            Regist regist=new Regist();
            regist.setIdentityNumber(sfzh);
            regist.setPassword(password);
            if (registService.valiUser(regist)!=null){
                isLogin=true;
            }
        }

        if (isUser==2){
            User user=new User();
            user.setLoginName(sfzh);
            user.setLoginPassword(password);
            if (userService.valiUser(user)!=null){
                isLogin=true;
            }
        }

        if(isLogin){
            HttpSession session=request.getSession();
            session.setAttribute("sfzh",sfzh);
            resp.getWriter().write("true");
        }else {
            resp.getWriter().write("false");
        }
    }
}


